package com.itheima.leyou.service;

import com.alibaba.fastjson.JSONObject;
import com.itheima.leyou.dao.UserDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {

    @Resource
    private UserDao userDao;

    public ArrayList<Map<String, Object>> getUser(String phone, String password){
        return userDao.getUser(phone, password);
    }

    @Transactional
    public Map<String, Object> insertUser(String jsonObj){

        Map<String, Object> map = new HashMap<>();
        JSONObject jsonObject = JSONObject.parseObject(jsonObj);

        if (jsonObject==null||jsonObject.isEmpty()){
            map.put("result", false);
            map.put("msg", "传入的参数有误，请刷新数据！");
            return map;
        }

        String username = jsonObject.getString("username");
        String phone = jsonObject.getString("phone");
        String password = jsonObject.getString("password");

        if (username == null||username.equals("")){
            map.put("result", false);
            map.put("msg", "用户名不能为空！");
            return map;
        }

        if (phone == null||phone.equals("")){
            map.put("result", false);
            map.put("msg", "电话号码不能为空！");
            return map;
        }

        return userDao.insertUser(username, phone, password);
    }
}
